//
//  ViewController.m
//  制作画板
//
//  Created by 伍银荣 on 2021/7/13.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ViewController.h"
#import "DrawingView.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISlider *slider;
@property (strong, nonatomic) IBOutlet DrawingView *drawingView;
@property (strong, nonatomic) IBOutlet UIButton *firstBtn;

@end

@implementation ViewController
//清屏
- (IBAction)clear:(id)sender {
    [self.drawingView clear];
}

//回退
- (IBAction)goBack:(id)sender {
    NSLog(@"回退");
    [self.drawingView goBack];
}
//橡皮
- (IBAction)eraser:(id)sender {
    [self.drawingView eraser];
}
//保存到相册
- (IBAction)save:(id)sender {
    //开启图片类型的图形上下文
    UIGraphicsBeginImageContextWithOptions(self.drawingView.bounds.size, NO, 0);
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //截图
    [self.drawingView.layer renderInContext:ctx];
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形类型的图形上下文
    UIGraphicsEndImageContext();
    //保存到相册
    UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //设置进度条的初始值
    self.drawingView.lineWidthValue = self.slider.minimumValue;
    [self btnClicked:self.firstBtn];
}

- (IBAction)sliderChangeValue:(UISlider *)sender {
   // NSLog(@"value = %f",sender.value);
    //把最新的值(线宽)赋值给DrawingView
    _drawingView.lineWidthValue = sender.value;
}

- (IBAction)btnClicked:(UIButton *)sender {
    self.drawingView.lineColor = sender.backgroundColor;
}

@end
